// mutations 业务层数据提交
import { toRaw } from 'vue';
import storage from '../utils/storage';

export default {
  // 设置用户登录信息
  saveUserInfo(state, userInfo) {
    state.userInfo = userInfo;
    storage.setItem('userInfo', userInfo);
  },
  // 通知数
  saveNoticeCount(state, noticeCount) {
    state.noticeCount = noticeCount;
    storage.setItem('noticeCount', noticeCount);
  },
  // 添加标签
  addTab(state, menuInfo) {
    state.activeTab = menuInfo;
    state.openedTabs.push(menuInfo);
    storage.setSessionItem('activeTab', menuInfo);
    storage.setSessionItem('openedTabs', toRaw(state.openedTabs));
  },
  // 改变标签
  changeTab(state, menuInfo) {
    state.activeTab = menuInfo;
    storage.setSessionItem('activeTab', menuInfo);
  },
  // 删除标签
  removeTab(state, targetName) {
    const tabs = state.openedTabs;
    let index = tabs.indexOf(tabs.filter((d) => d.name == targetName)[0]);
    let activeTab = tabs[index + 1] || tabs[index - 1];
    let name = state.activeTab.name;
    // 如果删除的是选中状态的标签，则设置上一级或下一级为选中状态否则不变
    if (activeTab && name == targetName) {
      state.activeTab = activeTab;
      storage.setSessionItem('activeTab', activeTab);
    }
    state.openedTabs.splice(index, 1);
    storage.setSessionItem('openedTabs', toRaw(state.openedTabs));
  },
  removeTabAll(state, menuInfo) {
    state.activeTab = menuInfo;
    state.openedTabs = [menuInfo];
    storage.clearSessionAll();
  },
  // 用户对应菜单权限
  saveUserMenu(state, menuList) {
    state.menuList = menuList;
    storage.setItem('menuList', menuList);
  },
  // 用户对应按钮权限
  saveUserAction(state, actionList) {
    state.actionList = actionList;
    storage.setItem('actionList', actionList);
  },
};
